<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Room extends Model
{
    use SoftDeletes;
    protected $table = 'rooms';
    protected $primarykey = 'id';
    protected $fillable = ['id','name','pic','notice','sort','type','main_id'];
    protected $dates = ['deleted_at'];
    public function users()
    {
        return $this->belongsToMany('App\Models\User','user_room','room_id','user_id');
    }

    public function chats()
    {
        return $this->hasMany('App\Models\Chat');
    }

    public function addUsers(array $ids=[]){
        if(!empty($ids)){
            return $this->users()->attach($ids);
        }
    }
    public function reduceUsers(array $ids=[]){
        if(!empty($ids)){
            return $this->users()->detach($ids);
        }
    }
    public function syncUsers($ids=[]){
        return $this->users()->sync($ids);
    }
}
